/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import controller.Action;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import persistence.ExtratoDAO;

/**
 *
 * @author Cristian
 */
public class ExibirExtratoAction implements Action {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response, Integer contaLogada) {


        String numero = contaLogada.toString();

        if (numero.equals("")) {
            try {
                response.sendRedirect("indexErro.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(ConfirmarLoginAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                ArrayList extratos = ExtratoDAO.getInstace().listarExtrato(Integer.parseInt(numero));

                if (extratos.isEmpty()) {
                    request.setAttribute("numeroConta", contaLogada);
                    RequestDispatcher dispatcher = request.getRequestDispatcher("semExtrato.jsp");
                    dispatcher.forward(request, response);
                } else {
                    request.setAttribute("extratos", extratos);
                    request.setAttribute("numeroConta", contaLogada);
                    RequestDispatcher dispatcher = request.getRequestDispatcher("imprimeExtrato.jsp");
                    dispatcher.forward(request, response);
                }

            } catch (Exception e) {
                try {
                    response.sendRedirect("indexErro.xhtml");
                } catch (IOException ex) {
                    Logger.getLogger(ConfirmarLoginAction.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    }
}
